import { createSlice } from '@reduxjs/toolkit';
import i18 from "i18next";

interface LanguageState {
  language: "en" | "zh",
  languageList: {name:string,code:string}[]
}

export const defaultLanguage: LanguageState = {
  language: "zh",
  languageList: [
    {name:"中文",code:"zh"},
    {name:"English",code:"en"},
  ],
}

// 创建一个 Slice
export const languageSlice = createSlice({
  name: 'counter',
  initialState:defaultLanguage,
  // 定义 reducers 并生成关联的操作
  reducers: {
    // 定义一个加的方法
    setLanguage: (state,action) => {
      i18.changeLanguage(action.payload.languageCode)
      state.language = action.payload.languageCode;
    },
  },
});

// 导出方法
export const { setLanguage } = languageSlice.actions;